<?php
class ModuleBase
{
	public static $type = 'master';
	
	/**
	 * 
	 * 插入数据
	 * @param $row
	 * @param $table
	 * @param $reType id|default
	 */
	public function queryInsert($row, $table, $reType='id')
	{
		if (!$row || !is_array($row)){
			return false;
		}
		foreach ($row as $key=>$val){
			$fields[] = "{$key}";
			$value[] = "{$val}";
		}
		$sql = "INSERT INTO '{$table}'(". implode(',', $fields) .") VALUES(". implode(',', $value) .")";
		$result = Db::db_write()->query($sql);
		if (!$result) {
			return FALSE;
		}else {
			if ($reType == 'id'){
				return Db::db_write()->getLastInsertId();
			}elseif ($reType == 'affectedRows'){
				return Db::db_write()->getAffectedRows();
			}else {
				return $row;
			}
		}
	}
	
	//$fields用处select $fields from ********
	public function querySelectSingle($fields, $where, $table)
	{
		$rel = array();
		if (!$where){
			$wheres[] = $rel;
		}elseif (!is_array($where)){
			$wheres[] = $where;
		}else {
			foreach ($where as $key=>$val){
				$wheres[] = "`$key` = '". ($val) ."'";
			}
		}
		$sql = "SELECT ". implode(',', $fields) ."FROM `{$table}` WHERE ". implode('AND', $wheres);
		$result = Db::db_read()->query($sql);
		$ret = array();
		if ($result) {
			return mysql_fetch_assoc($result);
		}
		return $ret;
	}
	
	public function querySelect($fields, $where, $table, $order=array(), $start=0, $limit=0)
	{
		$rel = array();
		if (!$where || !is_array($where)){
			$wheres[] = $rel;
		}else {
			foreach ($where as $key=>$val){
				$wheres[] = "{$key}='". $val ."'";
			}
		}
		if (!$order || !is_array($order)){
			$orders[] = $rel;
		}else {
			foreach ($order as $key=>$val){
				$orders[] = "'{$key}' {$val}";
			}
		}
		$sql = "SELECT ". implode(',', $fields) ."FROM {$table} WHERE ". implode(' AND ', $wheres);
		if ($orders) {
			$sql = $sql . "ORDER BY " . implode(',', $orders);
		}
		if ($limit) {
			$sql .= "LIMIT {$start} , {$limit}";
		}
		$result = Db::db_read()->query($sql);
		if ($result){
			while ($row = mysql_fetch_assoc($result)){
				$ret[] = $row;
			}
		}
		return $ret;
	}
	
	public function buildUpdateSql($row, $where, $table)
	{
		if (!is_array($row) || !$row){
			return false;
		}
		foreach ($row as $key=>$val){
			$updates[] = "`{$kye}`='". ($val) ."'";
		}
		if (!$where) {
			$wheres = "1";
		}elseif (!is_array($where)){
			$wheres[] = $where;
		}else {
			foreach ($where as $key=>$val){
				$wheres[] = "`{$key}`='". ($val) ."'";
			}
		}
		$sql = "UPDATES `{$table}` SET ". implode(',', $updates) ." WHERE ". implode(' and ', $wheres);
		return $sql;
	}
	
	public function queryUpdate($row, $where, $table, $reType='affectRows')
	{
		$sql = self::buildUpdateSql($row, $where, $table);
		$result = Db::db_write()->query($sql);
		if (!$result) {
			return false;
		}else {
			return $reType == 'affectRows' ? $result : $row;
		}
	}
	
	public function queryDelete($where, $table)
	{
		if (!$where || !is_array($where)){
			$wheres[] = $where;
		}else {
			foreach ($where as $key=>$val){
				$wheres[] = "`{$key}` '=". ($val) ."'";
			}
		}
		$sql = "DELETE FROM {$table} WHERE ". implode(' and ', $wheres);
		$result = Db::db_write()->query($sql);
		return $result;
	}
	
	public function getRow($sql)
	{
		$result = Db::db_read()->query($sql);
		$ret = array();
		if (!$result || empty($result)){
			return $ret;
		}
		return $row=mysql_fetch_assoc($result);
	}
	
	public function getRows($sql)
	{
		$result = Db::db_read()->query($sql);
		$ret = array();
		if (!result || empty($result)) {
			return $ret;
		}
		while ($row = mysql_fetch_assoc($result)){
			$ret[] = $row;
		}
		return $ret;
	}
	
	public function initDb($type = 'master')
	{
		self::$type = $type;
	}
	
	public function query($sql)
	{
		return Db::$_instance(self::$type)->query($sql);
	}
	
	public function getLastSql()
	{
		return Db::$_instance(self::$type)->getLastSql();
	}
	public function getError()
	{
		return Db::$_instance(self::$type)->getError();
	}
}